/*
 * author: tim
 * date: 9.20
 */
#define pi 3.1415926

QT_BEGIN_NAMESPACE
class time_control: public QWidget
{
    private:
        Ui::ui_time* ui;
        Time* now;
        Time* add;
        QTimer* timer;
    public:
        void step()
        {
            now->renew(1);
        }
        void draw4sec()
        {
            step();
            std::string pos("D:\\Originalcodes\\Field_of_C++\\cpp_class_first_1\\bg.jpg");
            cv::Mat bg(cv::imread(pos));
            int total = now->pass_info();
            double angle_h = ((double)(total % (60 * 60 * 24)) / (60 * 60)) / 24 * 2 * pi;
            double angle_m = ((double)(total % (60 * 60)) / 60 ) / 60 * 2 * pi;
            double angle_s = ((double)(total % 60)) / 60 * 2 * pi;
            std::cout<<(total % (60 * 60 * 24)) / (60 * 60)<<":"<<(total % (60 * 60)) / (60)<<":"<<(total % 60) <<std::endl;
            cv::cvtColor(bg, bg, cv::COLOR_BGR2RGB);
            cv::circle(bg, cv::Point(420, 520), 400, cv::Scalar(0, 0, 255), 8);
            cv::line(bg, cv::Point(420, 520), cv::Point(420 + 150 * std::sin(angle_h), 520 - 150 * std::cos(angle_h)), cv::Scalar(0, 0, 255), 10);
            cv::line(bg, cv::Point(420, 520), cv::Point(420 + 250 * std::sin(angle_m), 520 - 250 * std::cos(angle_m)), cv::Scalar(0, 0, 255), 10);
            cv::line(bg, cv::Point(420, 520), cv::Point(420 + 350 * std::sin(angle_s), 520 - 350 * std::cos(angle_s)), cv::Scalar(0, 0, 255), 10);//坐标计算


            QImage qbg = QImage((const unsigned char*)(bg.data),
                                     bg.cols,
                                     bg.rows,
                                     QImage::Format_RGB888
                                     );
            ui->label->setPixmap(QPixmap::fromImage(qbg.scaled(ui->label->size(), Qt::KeepAspectRatio)));
        }
        void change()
        {
            int h = stoi(ui->lineEdit->text().toStdString());
            int m = stoi(ui->lineEdit_2->text().toStdString());
            int s = stoi(ui->lineEdit_3->text().toStdString());
            int total = h * 3600 + m * 60 + s;

            add->renew(total);
            *now = *now + *add;
        }
        void reset()
        {
            std::time_t now_time = std::time(NULL);
            struct tm* local = std::localtime(&now_time);
            now->renew(- now->pass_info());
            now->renew(local->tm_hour * 60 * 60 + local->tm_min * 60 + local->tm_sec);
        }
        void clear_all()
        {
            ui->lineEdit->clear();
            ui->lineEdit_2->clear();
            ui->lineEdit_3->clear();
        }
        time_control(Ui::ui_time* _ui, Time* _now, Time* _add):
            ui(_ui), now(_now), add(_add), timer(new QTimer(this))
        {
            std::time_t now_time = std::time(NULL);
            struct tm* local = std::localtime(&now_time);

            now->renew(local->tm_hour * 60 * 60 + local->tm_min * 60 + local->tm_sec);
            timer->start(1000);

            QObject::connect(ui->pushButton, &QPushButton::clicked, this, &time_control::clear_all);
            QObject::connect(ui->pushButton_2, &QPushButton::clicked, this, &time_control::change);
            QObject::connect(ui->pushButton_3, &QPushButton::clicked, this, &time_control::reset);
            QObject::connect(timer, &QTimer::timeout, this, &time_control::draw4sec);
            QMetaObject::connectSlotsByName(this);
        }
};
QT_END_NAMESPACE